{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "640412c8",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-db2cbbc0c0e3>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-db2cbbc0c0e3>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    2.字典的重要属性\u001b[0m\n\u001b[1;37m      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# week8字典\n",
    "# 字典的初始（{key:value}）\n",
    "## 1.字典的文档(https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "## 2.字典的重要属性\n",
    "* 结构：key:value(键值对)\n",
    "* 字典是无序的，我们通过键（key）去查找值（value）,就好像list用索引（index）查找值（一样），只不过字典的“索引”被定义的名字。\n",
    "* 字典的key的定义有一定的要求：key是由“不可变”的数据结构才可以定义，list是不能作为key定义的\n",
    " ## 3.字典的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "86390d7c",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-5b7239db557d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m ]\n\u001b[0;32m      9\u001b[0m \u001b[0musers\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0musers_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"laji\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
     ]
    }
   ],
   "source": [
    "users = [\n",
    "    [\"laji\",20,\"student\"],\n",
    "    [\"xueba\",20,\"student\"]\n",
    "]\n",
    "users_dict = [\n",
    "    [\"laji\",20,\"student\"],\n",
    "    [\"xueba\",20,\"student\"]\n",
    "]\n",
    "users[1]\n",
    "users_dict[\"laji\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "670db063",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-3-83376b30b9c2>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-83376b30b9c2>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    users = [[\"laji\",20级,\"studennt\",没写作业],[\"xueba\",20级，\"student\",优秀作业]]\u001b[0m\n\u001b[1;37m                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "users = [[\"laji\",20级,\"studennt\",没写作业],[\"xueba\",20级，\"student\",优秀作业]]\n",
    "users_dict = [[\"laji\",20级，\"student\",没写作业]，[\"xueba\",20级，\"student\",优秀作业]]\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8bea37fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xueba', 20, 'student']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users = [\n",
    "    [\"laji\",20,\"student\"],\n",
    "    [\"xueba\",20,\"student\"]\n",
    "]\n",
    "users_dict = [\n",
    "    [\"laji\",20,\"student\"],\n",
    "    [\"xueba\",20,\"student\"]\n",
    "]\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7210fe99",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-51a187da00c3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[1;33m[\u001b[0m\u001b[1;34m\"xueba\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"student\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m ]\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0musers_dict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"xueba\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
     ]
    }
   ],
   "source": [
    "# users = [\n",
    "#    [\"laji\",20,\"student\"],\n",
    " #   [\"xueba\",20,\"student\"]\n",
    "#]\n",
    "users_dict = [\n",
    "    [\"laji\"，20，\"student\"],\n",
    "    [\"xueba\",20,\"student\"]\n",
    "]\n",
    "users_dict[\"xueba\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "17f388b6",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-17-f3b2930a1bcb>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-17-f3b2930a1bcb>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    1.info = {key1：value，key2：value2,...}直接按照字典格式创建\u001b[0m\n\u001b[1;37m      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#### 字典的创建（常用3种方式）\n",
    "1.info = {key1：value，key2：value2,...}直接按照字典格式创建\n",
    "2.info_02 = dict(key1=value,key2=value2,...)使用dict()进行字典创建\n",
    "3.运用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7e3ae189",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1': 'xiaoming', 'stu2': 'xiaohong', 'stu3': 'dawang,'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1\":\"xiaoming\",\n",
    "    \"stu2\":\"xiaohong\",\n",
    "    \"stu3\":\"dawang,\"\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "eb74d155",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1': 'xiaoming', 'stu2': 'xiaohong', 'stu3': 'dawang'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "stu1=\"xiaoming\",\n",
    "stu2=\"xiaohong\",\n",
    "stu3=\"dawang\",)\n",
    "\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "07aaacf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1': 'xiaoming', 'stu2': 'xiaohong', 'stu3': 'dawang'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 = {}# 新建空字典\n",
    "info_03[\"stu1\"] = \"xiaoming\"\n",
    "info_03[\"stu2\"] = \"xiaohong\"\n",
    "info_03[\"stu3\"] = \"dawang\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0afaf5b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 字典的增,删,改,查\n",
    " 1.字典的增加（和字典的第三种创建方式一样）\n",
    " * info_03"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
